﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DataAccess;

namespace Controller
{
    public class UserController:ControllerBase
    {
        public int CreateUser(UserEntity userEntity)
        {
            IList<User> users = (from u in Sdc.Users
                                 where u.UserName.ToUpper()==userEntity.UserName.ToUpper()
                                 select u).ToList();
            if(users.Count>0)
            {
                return 1;
            }
            else
            {
                User user = TransformUserEntityToUser(userEntity);

                Sdc.Users.InsertOnSubmit(user);
                Sdc.SubmitChanges();
                return 0;
            }
        }
        public static User TransformUserEntityToUser(UserEntity userEntity)
        {
            User user=new User();
            user.UserName = userEntity.UserName;
            user.Password = userEntity.Password;
            user.Email = userEntity.Email;
            user.FullName = userEntity.FullName;
            return user;
        }



        public int CheckUserExsiting(string userName)
        {
            IList<User> users = (from u in Sdc.Users
                                 where u.UserName.ToUpper() == userName
                                 select u).ToList();
            if (users.Count > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public int CheckLogin(string userName, string enPassword)
        {
            User user = (from u in Sdc.Users
                         where u.UserName.ToUpper() == userName
                         select u).SingleOrDefault();
            if(user.Password==enPassword)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public UserEntity FindUserByUserName(string userName)
        {
            User user = (from u in Sdc.Users
                         where u.UserName.ToUpper() == userName
                         select u).SingleOrDefault();
            UserEntity userEntity = TransforUserToUserEntity(user);
            return userEntity;
        }
        public static UserEntity TransforUserToUserEntity(User user)
        {
            UserEntity userEntity=new UserEntity();
            userEntity.UserName = user.UserName;
            userEntity.Password = user.Password;
            userEntity.Email = user.Email;
            userEntity.FullName = user.FullName;
            return userEntity;
        }

        public void UpdateUser(UserEntity userEntity)
        {
            User user = (from u in Sdc.Users
                         where u.UserName.ToUpper() == userEntity.UserName.ToUpper()
                         select u).SingleOrDefault();
            user.Password = userEntity.Password;
            user.Email = userEntity.Email;
            user.FullName = userEntity.FullName;
            Sdc.SubmitChanges();
        }
    }
}
